Hey welcome back.
In this lecture we're going to take a look at another global function in Lua called Loadstring.
This is a special function reserved for server scripts only and it can't be used by the client.
The purpose of the Loadstring is to take a string input and convert that string into a function that
can be executed.
So if you wanted to create a custom command line for players of a specific rank in your game, and you
want them to be able to input code and execute it on the server, you would use remotes and the Loadstring
function.
However, you have to be extremely careful with the Loadstring function.
If you do not ensure your remote functions are secure, exporters could execute server side code and
ruin your game.
You'd also want to make sure that anyone who has access to the Loadstring function can be trusted.
Otherwise, an individual could modify data stores in your game and just ruin a whole bunch of stuff.
So while the Loadstring function is neat, it can be very dangerous.
So to use the Loadstring function, you first must enable a property in the server script service called
Loadstring enabled.
When you hit the check box, a warning is going to pop up.
It's telling you activating the Loadstring enabled property might make your game vulnerable to exploits.
Are you sure you want to enable this?
We're going to hit yes, and it's telling you that it can make your game vulnerable.
If you set up the loadstring in a way where it can be exploited by somebody on the client.
But I'm just going to show you an example of how we can use the Loadstring function.
So I'm going to create a string here and I'm going to call it my string code.
And inside of here we can act like we're just scripting a regular script.
So I could put like a print statement in here and say something like, print hello, world.
And then I could also do something like create variables, and then I could use if statements to check
those variables and do something based on that, uh, condition.
And just you can do anything like you would with a regular script, write any code that you use would
execute.
Now you can take the string and you can put it into the load string function.
So load string is global.
And then you can pass the string which would be my string code.
And as long as the formatting or the syntax of the code in here matches Lua, you, then you can execute
it.
And the load string function returns back to us a function that can be executed.
So I can call it like some function, and then the string will get converted into code stored in this
function.
And that means we can go and call the function.
So if I call some function it should print out hello world.
And the bool was true.
So if we hit run.
There we go.
You see on the server hello world.
And the bool was true.
So this gives us the ability to execute code from a string.
So if you ever wanted to create your own custom command line and not use the one that's in the f nine
console, you could totally do it this way.
But again, you have to be very careful and make sure to secure remotes, because anybody who has the
ability to execute server side code can mess up a lot of things in your game.
But let's go ahead and look at another example of using the load string.
I'll create another string, call it my string code two.
And here something I could do like is create a variable.
I'll just call it player.
And we can just get a player randomly inside of the player service.
So I'll just find first child which is a player.
Or if there isn't one, then I could just wait for one to be added.
And then I can print out the name of this player.
So just like any other regular code in our game and again we can create a variable, I'll call it sum
function two.
And we can use the load string function and pass the string to create an executable function.
And then we can call this function.
And now we should get my name printed out into the console.
There we go.
Hello world!
The bool is true and Udemy lul you course.
So this was just a brief example of the load string function.
The use case for this function is pretty limited, but if you ever wanted to use it, the option is
there for you.
Just make sure to be careful with your remotes and not to trust people you don't trust with the function.
See you in the next lecture.